package com.dxl.lanju.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dxl.common.utils.Utils;
import com.dxl.lanju.dao.ReportDao;
import com.dxl.service.common.BaseService;
import com.lanju.bean.ShopStatBean;
import com.lanju.bean.ShopStatByShopBean;
import com.lanju.bean.ShopStatByShopDayDetailBean;
import com.lanju.bean.ShopVisitByAreaDayBean;
import com.lanju.bean.ShopVisitByShopDayBean;

@Service
@Transactional
public class ReportService extends BaseService {
	private static final Logger LOG = Logger.getLogger(ReportService.class);
	@Autowired
	private ReportDao reportDao;
	
	public Map<String, Object> shopStat() {
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		List<ShopStatBean> shopStat = reportDao.shopStat();
		List<Map<String, Object>> stat = new ArrayList<Map<String, Object>>();
		stat.add(new HashMap<String, Object>());
		stat.get(0).put("shopType", "合计：");
		if (Utils.isEmpty(shopStat)) {
			rtnMap.put("total", 0);
			rtnMap.put("rows", new ArrayList<ShopStatBean>());
			stat.get(0).put("amount", 0);
			rtnMap.put("footer", stat);
		} else {
			rtnMap.put("total", shopStat.size());
			rtnMap.put("rows", shopStat);
			BigDecimal total = new BigDecimal(0);
			for (ShopStatBean ss : shopStat) {
				if (Utils.isNotNull(ss.getAmount())) {
					total = total.add(ss.getAmount());
				}
			}
			stat.get(0).put("amount", total);
			rtnMap.put("footer", stat);
		}
		return rtnMap;
	}
	
	public Map<String, Object> shopStat(String shopId) {
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		List<ShopStatByShopBean> shopStat = reportDao.shopStat(shopId);
		List<Map<String, Object>> stat = new ArrayList<Map<String, Object>>();
		stat.add(new HashMap<String, Object>());
		stat.get(0).put("day", "合计：");
		if (Utils.isEmpty(shopStat)) {
			rtnMap.put("total", 0);
			rtnMap.put("rows", new ArrayList<ShopStatBean>());
			stat.get(0).put("amount", 0);
			rtnMap.put("footer", stat);
		} else {
			rtnMap.put("total", shopStat.size());
			rtnMap.put("rows", shopStat);
			BigDecimal total = new BigDecimal(0);
			for (ShopStatByShopBean ss : shopStat) {
				if (Utils.isNotNull(ss.getAmount())) {
					total = total.add(ss.getAmount());
				}
			}
			stat.get(0).put("amount", total);
			rtnMap.put("footer", stat);
		}
		return rtnMap;
	}
	
	public Map<String, Object> shopStat(String shopId, String day) {
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		List<ShopStatByShopDayDetailBean> shopStat = reportDao.shopStat(shopId, day);
		List<Map<String, Object>> stat = new ArrayList<Map<String, Object>>();
		stat.add(new HashMap<String, Object>());
		stat.get(0).put("prodSku", "合计：");
		if (Utils.isEmpty(shopStat)) {
			rtnMap.put("total", 0);
			rtnMap.put("rows", new ArrayList<ShopStatBean>());
			stat.get(0).put("sales", 0);
			stat.get(0).put("amount", 0);
			rtnMap.put("footer", stat);
		} else {
			rtnMap.put("total", shopStat.size());
			rtnMap.put("rows", shopStat);
			Integer sales = new Integer(0);
			BigDecimal amount = new BigDecimal(0);
			for (ShopStatByShopDayDetailBean ss : shopStat) {
				if (Utils.isNotNull(ss.getSales())) {
					sales += ss.getSales();
				}
				if (Utils.isNotNull(ss.getAmount())) {
					amount = amount.add(ss.getAmount());
				}
			}
			stat.get(0).put("sales", sales);
			stat.get(0).put("amount", amount);
			rtnMap.put("footer", stat);
		}
		return rtnMap;
	}
	
	public List<Map<String, Object>> visitDayList(String areaId) {
		return reportDao.visitDayList(areaId);
	}
	
	public Map<String, Object> shopVisit(String areaId, String day) {
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		List<ShopVisitByAreaDayBean> shopVisit = reportDao.shopVisit(areaId, day);
		List<Map<String, Object>> stat = new ArrayList<Map<String, Object>>();
		stat.add(new HashMap<String, Object>());
		stat.get(0).put("shopType", "合计：");
		if (Utils.isEmpty(shopVisit)) {
			rtnMap.put("total", 0);
			rtnMap.put("rows", new ArrayList<ShopStatBean>());
			stat.get(0).put("amount", 0);
			rtnMap.put("footer", stat);
		} else {
			rtnMap.put("total", shopVisit.size());
			rtnMap.put("rows", shopVisit);
			BigDecimal total = new BigDecimal(0);
			for (ShopVisitByAreaDayBean ss : shopVisit) {
				if (Utils.isNotNull(ss.getAmount())) {
					total = total.add(ss.getAmount());
				}
			}
			stat.get(0).put("amount", total);
			rtnMap.put("footer", stat);
		}
		return rtnMap;
	}
	
	public Map<String, Object> shopVisit(String areaId, String shopId, String day) {
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		List<ShopVisitByShopDayBean> shopStat = reportDao.shopVisit(areaId, shopId, day);
		List<Map<String, Object>> stat = new ArrayList<Map<String, Object>>();
		stat.add(new HashMap<String, Object>());
		stat.get(0).put("prodSku", "合计：");
		if (Utils.isEmpty(shopStat)) {
			rtnMap.put("total", 0);
			rtnMap.put("rows", new ArrayList<ShopStatBean>());
			stat.get(0).put("sales", 0);
			stat.get(0).put("amount", 0);
			rtnMap.put("footer", stat);
		} else {
			rtnMap.put("total", shopStat.size());
			rtnMap.put("rows", shopStat);
			Integer sales = new Integer(0);
			BigDecimal amount = new BigDecimal(0);
			for (ShopVisitByShopDayBean ss : shopStat) {
				if (Utils.isNotNull(ss.getSales())) {
					sales += ss.getSales();
				}
				if (Utils.isNotNull(ss.getAmount())) {
					amount = amount.add(ss.getAmount());
				}
			}
			stat.get(0).put("sales", sales);
			stat.get(0).put("amount", amount);
			rtnMap.put("footer", stat);
		}
		return rtnMap;
	}
}
